#### PACKAGES ####
library(ggplot2)
library(dplyr)
library(RVAideMemoire)
library(stargazer)

sink("../output/lucid_experiment.log")
#### Read in Data ####
lucid_poll <- read.csv("../input/poll_2022_weighted_final_topost.csv")

#### Create Variables ####

#time for respondents who saw individual pages

lucid_poll$individualtiming <- lucid_poll$instruction_timing_Page.Submit + lucid_poll$mandmin_timing_Page.Submit + lucid_poll$bodycam_timing_Page.Submit + lucid_poll$increase_timing_Page.Submit + lucid_poll$decrease_timing_Page.Submit + lucid_poll$chokeholds_timing_Page.Submit + lucid_poll$registry_timing_Page.Submit + lucid_poll$surplus_timing_Page.Submit

#single combined time variable for all respondents 

lucid_poll$combinedtime <- 0
lucid_poll$combinedtime[!is.na(lucid_poll$individualtiming)] <- lucid_poll$individualtiming[!is.na(lucid_poll$individualtiming)]
lucid_poll$combinedtime[!is.na(lucid_poll$policinggrid_timing_Page.Submit)] <- lucid_poll$policinggrid_timing_Page.Submit[!is.na(lucid_poll$policinggrid_timing_Page.Submit)]

# classify Inconsistent and non-Inconsistent

lucid_poll$Inconsistent <- ifelse(lucid_poll$Grid.Question_3 == 1 & lucid_poll$Grid.Question_4 ==1, "Inconsistent", "Consistent")
lucid_poll$Inconsistent[lucid_poll$increasefunds==1 &lucid_poll$decreasefunds==1] <- "Inconsistent"
lucid_poll$Inconsistent[is.na(lucid_poll$Inconsistent)] <- "Consistent"


# variable for type of treatment

lucid_poll$Experiment[!is.na(lucid_poll$increasefunds)] <- "Single Question"
lucid_poll$Experiment[!is.na(lucid_poll$Grid.Question_4)] <- "Grid"

#### TABLE B8: Inconsistency ####
print("Table B8: Inconsistent by experimental group")
prop.table(table(lucid_poll$Experiment, lucid_poll$Inconsistent), 1)


#### FIGURE 5: SPEED #### 

lucid_poll$Inconsistent <- factor(lucid_poll$Inconsistent, levels = c("Inconsistent", "Consistent"))
experiment_time <- ggplot(data = subset(lucid_poll, !is.na(lucid_poll$Experiment)), aes(Inconsistent, combinedtime, fill=Experiment)) +
  scale_x_discrete(name="Type of Answer") +
  scale_y_continuous(name="Median time spent on policing questions (sec)") +
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_bar(position = "dodge",
           stat = "summary",
           fun = "median") +
  scale_fill_brewer("Question Type", labels=c("Grid", "Single Question"), palette="Dark2") + theme_bw()
ggsave("../output/lucidtiming.pdf",
       plot = experiment_time,
       width = 7,
       height = 6)
print("Figure 5 created")
print("Median test for differences in timing")
#### median test #####
mood.medtest(combinedtime ~ Experiment,
             data  = subset(lucid_poll, Inconsistent == "Inconsistent"),
             exact = FALSE)
mood.medtest(combinedtime ~ Experiment,
             data  = subset(lucid_poll, Inconsistent == "Consistent"),
             exact = FALSE)
# Numbers for inclusion in the text
print("Timing, Grid & Inconsistent")
summary(lucid_poll$combinedtime[lucid_poll$Experiment=="Grid" & lucid_poll$Inconsistent == "Inconsistent"])
print("Timing, Grid & Consistent")
summary(lucid_poll$combinedtime[lucid_poll$Experiment=="Grid" & lucid_poll$Inconsistent == "Consistent"])
print("Timing, Paginated & Inconsistent")
summary(lucid_poll$combinedtime[lucid_poll$Experiment=="Single Question" & lucid_poll$Inconsistent == "Inconsistent"])
print("Timing, Paginated & Consistent")
summary(lucid_poll$combinedtime[lucid_poll$Experiment=="Single Question" & lucid_poll$Inconsistent == "Consistent"])
print("Timing, grid")
summary(lucid_poll$combinedtime[lucid_poll$Experiment=="Grid"])
print("Timing, paginated")
summary(lucid_poll$combinedtime[lucid_poll$Experiment=="Single Question"])

#### Table B9: Straightlining Evidence ####
print("Table B9:")
classpoliceboth <- data.frame(lucid_poll$surplus, lucid_poll$increasefunds, lucid_poll$decreasefunds, lucid_poll$mand_min, lucid_poll$bodycam, lucid_poll$chokeholds, lucid_poll$registry, lucid_poll$Grid.Question_1, lucid_poll$Grid.Question_2, lucid_poll$Grid.Question_3, lucid_poll$Grid.Question_4, lucid_poll$Grid.Question_5, lucid_poll$Grid.Question_6, lucid_poll$Grid.Question_7)
#coded as 1s and 2s now, need it to be 0 and 1. define function to do that.
subtraction <- function(observed) {
  
  result <- observed - 1
}

#recode.
classpoliceboth <- classpoliceboth %>% 
  select_if(is.numeric) %>% 
  mutate_all( ~ subtraction(.))


#break out the treatment conditions.

Inconsistent<- lucid_poll$Inconsistent
Experiment <- lucid_poll$Experiment
#sum the new rows to generate straightlining variable.
classpoliceboth$sum <- rowSums(classpoliceboth, na.rm=TRUE)
#create new df
straightlining_df <- cbind(classpoliceboth, Inconsistent, Experiment) 
straightlining_df <- subset(straightlining_df, subset = !is.na(Experiment))

# Produce stats for Table #
print("Straightlining, Grid and Inconsistent")
summary(straightlining_df$sum[straightlining_df$Experiment=="Grid" & straightlining_df$Inconsistent == "Inconsistent"])
print("Straightlining, Grid and Consistent")
summary(straightlining_df$sum[straightlining_df$Experiment=="Grid" & straightlining_df$Inconsistent == "Consistent"])
print("Straightlining, Paginated and Inconsistent")
summary(straightlining_df$sum[straightlining_df$Experiment=="Single Question" & straightlining_df$Inconsistent == "Inconsistent"])
print("Straightlining, Paginated and Consistent")
summary(straightlining_df$sum[straightlining_df$Experiment=="Single Question" & straightlining_df$Inconsistent == "Consistent"])